﻿import java.io.*;

public class Serializable {
	public static void main(String[] args) {

		Student stu=new Student("马三炮",15,"男");
		

		OutputStream os=null;
		ObjectOutputStream oos=null;
		

		FileInputStream is=null;
		ObjectInputStream ois=null;

		try {
			os=new FileOutputStream("d:/1.bin");
			oos=new ObjectOutputStream(os);
			oos.writeObject(stu);

			is =new FileInputStream("d:/1.bin");
			ois=new ObjectInputStream(is);
			try {
				Student stu2=(Student)ois.readObject();
				System.out.println("学生姓名:"+stu2.getName()+"年龄:"+stu2.getAge()+"性别:"+stu2.getGender());
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block

				e.printStackTrace();
			}
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block

			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		}finally{
			if(oos!=null){
				try {
					oos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block

					e.printStackTrace();
				}
			}
			if(os!=null){
				try {
					os.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block

					e.printStackTrace();
				}
			}
			if(ois!=null){
				try {
					ois.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block

					e.printStackTrace();
				}
			}
			if(is!=null){
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block

					e.printStackTrace();
				}
			}
		}
	}
}